package com.tiny.persistence.repository.impl;

import java.util.List;
import java.util.Map;

import javax.persistence.Query;

import org.apache.log4j.Logger;

import com.tiny.persistence.domain.WxRoleMenu;
import com.tiny.persistence.repository.plus.WxRoleMenuRepoPlus;
/**
 *前台角色菜单关系仓储层
* @ClassName:  
* @Description: TODO 
* @author:  
* @date: 2014年3月23日
*
 */

public class WxRoleMenuRepoImpl extends BaseRepoImpl<WxRoleMenu> implements WxRoleMenuRepoPlus{

    private static Logger logger = Logger.getLogger(WxRoleMenuRepoImpl.class);

    @Override
    public List<WxRoleMenu> getListByCondition(Map<String, Object> condition) {
        Query query = em.createQuery("from WxRoleMenu as re");
        @SuppressWarnings("unchecked")
		List<WxRoleMenu> list = query.getResultList();
        logger.info("======= " + list);
        return list;
    }

    @Override
    public int getCountByCondition(Map<String, Object> condition) {
        
        Query query = em.createQuery("select count(*) from WxRoleMenu as au");
        Object obj = query.getSingleResult();
        
        logger.info("======= count=" + obj);
        
        return Integer.parseInt(obj.toString());
    }

    /**
     * 根据角色id删除相关的权限
     * DELETE FROM Table z WHERE z.name = :name
     */
    public int deleteByRoleId(int roleId) {
        Query query = em.createNativeQuery("delete from wx_role_menu where wx_role_id =:roleId ");
        query.setParameter("roleId", roleId);
        return query.executeUpdate();
        
    }
    
	public List<WxRoleMenu> findSelectedMenuList(int roleId) {
		Query query = em.createQuery(" from WxRoleMenu o where o.wxRoleId=:roleId");
		query.setParameter("roleId", roleId);
		@SuppressWarnings("unchecked")
		List<WxRoleMenu> list = query.getResultList();

		logger.info("======= " + list);

		return list;
	}

	@SuppressWarnings("unchecked")
	public List<WxRoleMenu> getByKeyword(int roleId, String eventKey) {
		Query query = em.createQuery(" from WxRoleMenu o where o.wxRoleId=:roleId and o.wxMenuId in(select id from WxMenu where eventKey=:eventKey)");
		query.setParameter("eventKey", eventKey);
		query.setParameter("roleId", roleId);
		return query.getResultList();
	}

}
